<template>
  <!-- 基本渲染
       1：定义api
       2:导入
       3：进入页面调用
       4：存储数据并渲染

       上拉加载
         van-list
            v-model
            finished
            finished-text
            @load
               发请求
               将v-model值改成false
               数据累加
               修改下一页请求条件
               判断是否加载 完成，如果完成finished=true

       下拉刷新
          van-pull-refresh
             v-model
             @refresh="
                    还原数据与组件
                    v-model的值变成false
             "

-->

  <div class="search-result">
    <van-nav-bar
      :title="$route.params.key + '的搜索结果'"
      left-arrow
      fixed
      placeholder
    ></van-nav-bar>
    <div class="list">
      <van-pull-refresh v-model="isLoading" @refresh="refresh">
        <van-list
          @load="getData"
          finished-text="没有更多了"
          v-model="loading"
          :finished="finished"
          v-if="bol"
        >
          <van-cell
            v-for="item in list"
            :key="item.art_id"
            :title="item.title"
          ></van-cell>
        </van-list>
      </van-pull-refresh>
    </div>
  </div>
</template>
<script>
import { search } from '@/api/search'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      isLoading: false,
      bol: true
    }
  },
  created () {
    // this.getData()
  },
  methods: {
    async getData () {
      const res = await search({
        page: this.page,
        per_page: 10,
        q: this.$route.params.key
      })
      this.loading = false
      this.list.push(...res.data.results)
      this.page++
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }

      console.log('搜索列表', res)
    },
    refresh () {
      this.list = []
      this.loading = false
      this.finished = false
      this.page = 1
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
        this.isLoading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.list {
  position: fixed;
  left: 0;
  top: 46px;
  bottom: 0;
  width: 100vw;
  overflow: auto;
}
</style>
